const express = require('express');
const app = express();
const mongoose = require('./db');
const bodyParser = require('body-parser');
/*配置bodyparser用来接收post的数据*/
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

/*定义一个用户名密码的表*/
const userSchema = mongoose.Schema({
    username:String,
    password:String,
    anquanma:String
});
const userModel = mongoose.model('userModel',userSchema,"user")
app.get('/:name/info',function (req,res) {
    //获取数据
    userModel.findOne({username:req.params.name},function (err,result) {
        if(!err){
            res.send(result)
        }
    })
})

app.post('/',function (req,res) {
    //添加数据
    /*req.body就是客户端post上来的数据*/
    const username = req.body.username
    const password = req.body.password;
    const anquanma = req.body.anquanma;
    userModel.create({username:username,password:password,anquanma:anquanma},function (err,doc) {
        if(!err){
            res.json({success:true})
        }
    })
})

/*先查找到这条数据再删除这条数据*/
app.get('/:name/delete',function (req,res) {
    userModel.findOne({username:req.params.name},function (err,doc) {
        if(!err){
            doc.remove(function (err) {
                if(!err){
                    res.send('删除成功')
                }
            })
        }
    })
})
app.listen(80,'127.0.0.1');